package com.payment.notify.receive.datasource;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.payment.common.core.datasource.DataSourceType;
import com.payment.common.core.datasource.DynamicDataSource;

/**
 * 类名：DataSourceConfig.java<br>
 * 描述：多数据源<br>
 * 创建者：XINJF<br>
 * 创建日期：2018年2月28日 下午8:18:17<br>
 * 版本：1.0<br>
 * 修改者：<br>
 * 修改日期：<br>
 */
@Configuration
@MapperScan(basePackages={"com.payment.notify.receive.repository","com.payment.notify.receive.repository.*"})  
@EnableTransactionManagement
public class DataSourceConfig{
	
	@Bean(name = "masterDataSource")
	@Primary
    @ConfigurationProperties(prefix="spring.datasource.master")
	public DataSource masterDataSource() {
		System.out.println("masterDataSource");
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix="spring.datasource.slave")
	public DataSource slaveDataSource() {
		System.out.println("slaveDataSource");
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name="dynamicDataSource")
    @Primary
	public DataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource master,
			@Qualifier("slaveDataSource") DataSource slave) {
		DynamicDataSource dynamicDataSource = new DynamicDataSource();
		//配置多数据源
		Map<Object,Object> map = new HashMap<>();
		map.put(DataSourceType.MASTER, master);   //key需要跟ThreadLocal中的值对应  
        map.put(DataSourceType.SLAVE, slave);  
        dynamicDataSource.setTargetDataSources(map); 
        //设置默认数据源 
        dynamicDataSource.setDefaultTargetDataSource(master);
        return dynamicDataSource;  
	}
	
//	@Bean
//    public SqlSessionFactory sqlSessionFactory(DynamicDataSource ds) throws Exception {
////        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//
//        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
//        fb.setDataSource(ds);// 指定数据源(这个必须有，否则报错)
//        // 下边两句仅仅用于*.xml文件，如果整个持久层操作不需要使用到xml文件的话（只用注解就可以搞定），则不加
////        fb.setTypeAliasesPackage("com.huan.model");// 指定基包
////        fb.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml"));//
//
//        return fb.getObject();
//}
	@Bean
	    public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource master,
	                                                @Qualifier("slaveDataSource") DataSource slave) throws Exception{
	         SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
	         fb.setDataSource(this.dynamicDataSource(master, slave));
//	         fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));
	         fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/geeboo/repository/*Mapper.xml"));
	         return fb.getObject();
	     }
	
	
	 @Bean
	 public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) throws Exception {
	   return new DataSourceTransactionManager(dataSource);
	 }

}
